package org.southyin.service.impl;

import org.southyin.commons.CommonsPage;
import org.southyin.commons.CommonsResp;
import org.southyin.dao.CmsArticlDao;
import org.southyin.dao.impl.CmsArticlDaoImpl;
import org.southyin.model.CmsArticle;
import org.southyin.service.CmsArticleService;

import java.util.List;

/**
 * @ClassName CmsArticleServiceImpl
 * @Description TODO
 * @Author hasee
 * @Date 2021/10/29
 * @Version 1.0
 **/
public class CmsArticleServiceImpl implements CmsArticleService {
    CmsArticlDao dao = new CmsArticlDaoImpl();
    public CommonsResp saveArticleInfo(CmsArticle cmsArticle) {
        int i = dao.saveArticleInfo(cmsArticle);
        if (i == 0){
            return CommonsResp.failed();
        }
        return CommonsResp.success();
    }

    public CommonsPage<CmsArticle> listHotOrNewOrHotList(String listId, int pageNum, int pageSize) {
        List<CmsArticle> cmsArticles = dao.listHotOrNewOrHotList(listId,pageNum,pageSize);
        int total = dao.getTotalCounsOfArticles(null);
        CommonsPage<CmsArticle> commonsPage = new CommonsPage<CmsArticle>();
        commonsPage.setTotal(total);
        commonsPage.setList(cmsArticles);
        return commonsPage;
    }

    public CmsArticle getArticleById(Long id) {
        CmsArticle article = dao.getArticleById(id);
        if (article != null) {
            dao.updateArticleViews(id);
            return article;
        }
        return null;
    }
}
